<template>
  <el-dialog
      title="新增文章"
      :visible="value"
      @close="close"
      width="60%"
    >
    <el-form :model="ruleForm" :rules="rules" ref="formRef" label-width="100px" class="demo-ruleForm">
  <el-form-item label="文章标题" prop="title">
    <el-input v-model="ruleForm.title" placeholder="请输入文章标签"></el-input>
  </el-form-item>
  <el-form-item style="height: 330px;" label="文章内容" prop="articleBody">
    <quill-editor style="height:200px"  v-model="ruleForm.articleBody"></quill-editor>
  </el-form-item>
  <el-form-item  label="视频地址" >
    <el-input v-model="ruleForm.videoURL" placeholder="请输入视频地址"></el-input>
  </el-form-item>
  <el-form-item style="text-align: right;">
    <el-button @click="resetForm()">取消</el-button>
    <el-button type="primary" @click="submitForm()">确定</el-button>
  </el-form-item>
  </el-form>
    </el-dialog>
</template>
<script>
import { addArticlesRequest, getArticlesDetailRequest, editArticlesRequest } from '../../api/hmmm/articles-list'
export default {
  props: {
    value: {
      type: Boolean,
      required: true
    },
    id: {
      type: Number,
      required: true
    }
  },
  data () {
    return {
      ruleForm: {
        title: '',
        articleBody: '',
        videoURL: ''
      },
      rules: {
        title: [
          { required: true, message: '请输入文章标题', trigger: 'blur' },
          { min: 1, max: 100, message: '长度在 1 到 50 个字符', trigger: 'blur' }
        ],
        articleBody: [
          { required: true, message: '请输入文章内容', trigger: 'blur' },
          { min: 1, max: 10000, message: '长度在 1 到 10000 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    if (this.id) {
      this.getArticlesDetail()
    }
  },
  methods: {
    async getArticlesDetail () {
      const res = await getArticlesDetailRequest(this.id)
      this.ruleForm.title = res.data.title
      this.ruleForm.articleBody = res.data.articleBody
      this.ruleForm.videoURL = res.data.videoURL
    //
    },
    submitForm () {
      this.$refs.formRef.validate(async valid => {
        if (valid) {
          if (!this.id) {
            console.log(valid)
            await addArticlesRequest({ ...this.ruleForm, id: null })
            this.$message.success('添加成功')
            this.close()
            this.$emit('addSuccess')
          } else {
            await editArticlesRequest({ ...this.ruleForm, id: this.id })
            this.$message.success('修改成功')
            this.close()
            this.$emit('addSuccess')
          }
        }
      })
    },
    close () {
      this.$emit('input', false)
    },
    resetForm () {
      this.$refs.formRef.resetFields()
      this.close()
    }
  }
}
</script>
